{template '_header'}
<div class="page-heading"> <h2>京东助手</h2> </div>
<div class='alert alert-danger'>1.尽量在服务器空闲时间来操作，会占用大量内存与带宽，在获取过程中，请不要进行任何操作!</br>
    2.由于京东商品规格为不同链接的商品，所以京东助手无法获取京东商品内的其他规格!
    </br>3.暂不支持全球购等海外商品链接
</div>

<form id="dataform" action="" method="post" class="form-horizontal form">

    <div class="form-group">
        <label class="col-sm-2 control-label must">链接 或 itemId</label>
        <div class="col-sm-9">
            <textarea style="width:600px;height:200px" id="url" name="url" class="form-control"></textarea>
            <span class="help-block">例如商品链接为: http://item.jd.com/1856582.html,直接输入商品链接或输入商品ID:1856582</span>
            <span class="help-block">每行仅限输入一个链接或一个商品ID可多行输入</span>
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">设置分类</label>

        <div class="col-sm-9">
            {if $shopset['catlevel']==3}
            {php echo tpl_form_field_category_3level('category', $parent, $children,0,0,0)}
            {else}
            {php echo tpl_form_field_category_2level('category', $parent, $children,0,0)}
            {/if}
        </div>

    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label"> </label>
        <div class="col-sm-9">
            <span class="help-block">此分类读取的是商城的商品分类, 设置默认抓取商品的分类</span>
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label"> </label>
        <div class="col-sm-9">
            <input id="btn_submit" type="button"  value="立即获取" class="btn btn-primary col-lg-12"  onclick="formcheck()"/>
        </div>
    </div>

</form>

<script type="text/javascript">

    var category = {php echo json_encode($children)};

    var len = 0;
    var urls = [];
    var total = 0;
    function formcheck() {

        if ($(":input[name='url']").val() == '') {
            tip.msgbox.err("请输入商品链接或itemId!");
            $(":input[name='url']").focus();
            return;
        }
        $("#dataform").attr("disabled", "true");
        $("#btn_submit").val("正在获取中...").removeClass("btn-primary").attr("disabled", "true");

        urls = $("#url").val().split('\n');
        total = urls.length;
        $("#btn_submit").val("检测到需要获取 " + total + " 个宝贝, 请等待开始....");
        fetch_next();
        return;
    }
    function fetch_next() {
        var postdata =  {
            url: urls[len],
            pcate: $("select[name='category[parentid]']").val(),
            ccate: $("select[name='category[childid]']").val()
        };
        {if $shopset['catlevel']==3}
        postdata.tcate = $("select[name='category[thirdid]']").val();
        {/if}

        $.post("{php echo merchUrl('taobao/jingdong/fetch')}",
                postdata,
                function (data) {

                    len++;
                    $("#btn_submit").val("已经获取  " + len + " / " + total + " 个宝贝, 请等待....");

                    if (len >= total) {
                        $("#btn_submit").val("立即获取").addClass("btn-primary").removeAttr("disabled");
                        if (confirm('商品已经获取成功, 是否跳转到商品管理?')) {
                            location.href = "{php echo merchUrl('goods' ,array('goodsfrom'=>stock))}";
                        }
                        else {
                            location.reload();
                        }
                    }
                    else {
                        fetch_next();
                    }

                }, "json");
    }
</script>

{template '_footer'}
